#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
查看财联社电报数据
"""

import sqlite3
from datetime import datetime


def view_telegrams(db_path="telegrams.db", limit=20):
    """查看电报数据"""
    try:
        conn = sqlite3.connect(db_path)
        cursor = conn.cursor()
        
        # 查询最新的电报数据
        cursor.execute('''
            SELECT telegram_id, time, title, content, reading_num, url
            FROM telegrams 
            ORDER BY time DESC 
            LIMIT ?
        ''', (limit,))
        
        results = cursor.fetchall()
        
        print(f"=== 财联社电报数据（最新{len(results)}条）===")
        print(f"{'ID':<8} {'时间':<20} {'标题':<50} {'阅读数':<10}")
        print("-" * 100)
        
        for row in results:
            telegram_id, time_str, title, content, reading_num, url = row
            # 截取标题前50个字符
            short_title = title[:47] + "..." if len(title) > 50 else title
            print(f"{telegram_id:<8} {time_str:<20} {short_title:<50} {reading_num:<10}")
            
        conn.close()
        
        # 显示总数
        conn = sqlite3.connect(db_path)
        cursor = conn.cursor()
        cursor.execute('SELECT COUNT(*) FROM telegrams')
        total = cursor.fetchone()[0]
        conn.close()
        
        print(f"\n总记录数: {total}")
        
    except Exception as e:
        print(f"查看数据时出错: {e}")


if __name__ == "__main__":
    view_telegrams()